rm(list = ls())
load("data/gdelt_matrix.RData")
library(amen)
## run three AME models
results <- names(gdelt_matrix)
AME_score_gdelt <- list()

for (i in 1:length(names(gdelt_matrix))){
  AME_score_gdelt[[i]] = ame(
    Y = as.matrix(gdelt_matrix[[i]]), 
    symmetric= FALSE, 
    #nvar=TRUE, 
    rvar=TRUE,
    cvar = TRUE,
    R=1, 
    model = "nrm",
    #family='nrm', 
    intercept=TRUE, seed=6886,
    burn= 5000, nscan= 20000, odens=20,
    plot=FALSE, gof=TRUE )
  print(i)
}
names(AME_score_gdelt) <- names(gdelt_matrix)
save(AME_score_gdelt, file = "data/AME_score_gdelt.RData")



load("data/AME_score_gdelt.RData")
library(purrr)
library(reshape)
library(dplyr)
# create a list to store the data
ame_df_gdelt <- list()

for (i in seq_along(AME_score_gdelt)){
  #extract the model results
  m <-  AME_score_gdelt[[i]]
  # get a name of the model
  yearmonth <- names(AME_score_gdelt)[i]
  
  l <- melt(m$UVPM) 
  names(l) <- c("iso_a", "iso_b", "latent_score_gdelt")
  
  l$iso_a <- as.character(l$iso_a )
  l$iso_b <- as.character(l$iso_b)
  
  l <- l %>% 
    dplyr::mutate(latent_score_gdelt = ifelse(iso_a == iso_b, NA, latent_score_gdelt)) %>% 
    rowwise() %>% 
    dplyr::mutate(yearmonth = yearmonth)
  
  ame_df_gdelt[[i]] <- l
}
names(ame_df_gdelt) <- names(AME_score_gdelt)

ame_df_gdelt <- do.call(rbind, ame_df_gdelt)
ame_df_gdelt$year <- substr(ame_df_gdelt$yearmonth,1, 4)
ame_df_gdelt$month <- substr(ame_df_gdelt$yearmonth,5, 6)
ame_df_gdelt$year <- as.integer(ame_df_gdelt$year)
ame_df_gdelt$month <- as.integer(ame_df_gdelt$month)
save(ame_df_gdelt, file = "data/ame_df_gdelt.RData")


## merge with icews
load("data/ame_df_icews.RData")
latent_df <- full_join(ame_df_gdelt, ame_df_icews, 
                       by = c("iso_a","iso_b", "yearmonth", "year","month"))
save(latent_df, file = "data/latent_df.RData")
summary(lm(latent_score_gdelt ~ latent_score, data = latent_df))
ggplot(data = latent_df, aes(x = latent_score_gdelt, 
                             y = latent_score)) + geom_line()
